package com.jade.platform.admin.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jade.platform.admin.api.entity.request.UserInfoQueryReq;
import com.jade.platform.admin.api.entity.vo.UserInfoVo;
import com.jade.platform.admin.entity.UserInfo;
import com.jade.platform.admin.mapper.UserInfoMapper;
import com.jade.platform.admin.service.IUserInfoService;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 用户信息表 服务实现类
 * </p>
 *
 * @author Arnold
 * @since 2022-07-06 19:09:29
 */
@Service("userInfoService")
public class UserInfoServiceImpl extends ServiceImpl<UserInfoMapper, UserInfo> implements IUserInfoService {

    @Override
    public Page<UserInfoVo> page(UserInfoQueryReq userInfoQueryReq) {
        Page page =BeanUtil.toBean(userInfoQueryReq.getSearch(), Page.class);
        LambdaQueryWrapper<UserInfo> wrapper = new LambdaQueryWrapper<>();
        if (null != userInfoQueryReq.getUserStatus()) {
            if (StrUtil.isNotBlank(userInfoQueryReq.getSearch().getKeyword())) {
                wrapper.eq(UserInfo::getUserStatus, userInfoQueryReq.getUserStatus())
                        .and(i -> i.likeRight(UserInfo::getUserName, userInfoQueryReq.getSearch().getKeyword())
                                .or().likeRight(UserInfo::getFullName, userInfoQueryReq.getSearch().getKeyword())
                                .or().likeRight(UserInfo::getEmail, userInfoQueryReq.getSearch().getKeyword())
                                .or().likeRight(UserInfo::getMobile, userInfoQueryReq.getSearch().getKeyword()));
            } else {
                wrapper.eq(UserInfo::getUserStatus, userInfoQueryReq.getUserStatus());
            }
        } else if (StrUtil.isNotBlank(userInfoQueryReq.getSearch().getKeyword())) {
            wrapper.likeRight(UserInfo::getUserName, userInfoQueryReq.getSearch().getKeyword())
                    .or().likeRight(UserInfo::getFullName, userInfoQueryReq.getSearch().getKeyword())
                    .or().likeRight(UserInfo::getEmail, userInfoQueryReq.getSearch().getKeyword())
                    .or().likeRight(UserInfo::getMobile, userInfoQueryReq.getSearch().getKeyword());
        }
        wrapper.orderByDesc(UserInfo::getUpdateTime);
        return (Page<UserInfoVo>) page(page,wrapper).convert(list-> BeanUtil.toBean(list, UserInfoVo.class));
    }
}
